﻿var CLIPBOARD = "";
function Activity(container) {
    this.Container = container;
    this.AddTask = function () {
        if (instance != null) {
            var activityId = GetNextActivityId();
            $(this.Container).append("<div activityType=\"Task\" class=\"w\" id=\"" + activityId + "\">" + activityId + "<div class=\"ep\"></div></div>");
            
            this.RenderWorkflow();
        }
    }
    this.AddDecision = function ()
    {
        if (instance != null) {
            var activityId = GetNextActivityId();
            $(this.Container).append("<div activityType=\"TaskDecision\" class=\"Decision\" id=\"" + activityId + "\"><div class=\"ep epYes\"></div><div class=\"ep epNo\"></div></div>");
            this.RenderWorkflow();
        }
    };

    this.RenderWorkflow = function()
    {
        var windows = jsPlumb.getSelector(".statemachine-demo .w,.statemachine-demo .WorkflowStart,.statemachine-demo .Decision");
        instance.makeSource(windows, {
            filter: ".ep",				// only supported by jquery
            anchor: "Continuous",
            connector: ["Flowchart", { curviness: 10 ,cornerRadius :3 }],
            connectorStyle: { strokeStyle: "#ff0000", lineWidth: 2, outlineColor: "transparent", outlineWidth: 4  },
            maxConnections: 5,
            onMaxConnections: function (info, e) {
                alert("Maximum connections (" + info.maxConnections + ") reached");
            }
        });

        instance.makeTarget(windows, {
            dropOptions: { hoverClass: "dragHover" },
            anchor: "Continuous"
        });

        windows = jsPlumb.getSelector(".statemachine-demo .w,.statemachine-demo .Decision");
        instance.draggable(windows);
        $(document).contextmenu({
            delegate: ".w",
            preventSelect: true,
            taphold: true,
            menu: [
                { title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors" },
                { title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy" },
                { title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true },
                { title: "----" },
                { title: "Properties", cmd: "properties" },
            ],
            // Handle menu selection to implement a fake-clipboard
            select: function (event, ui) {
                var $target = ui.target;
                switch (ui.cmd) {
                    case "copy":
                        CLIPBOARD = $target.text();
                        break
                    case "paste":
                        CLIPBOARD = "";
                        break
                    case "properties":
                        // click action here
                        var activityType = $target.attr("activityType");
                        var activityForm = new ActivityForm(activityType);
                        activityForm.LoadFields();
                        var activityDailog = new ActivityDailog($target,".popup",".popupForm",activityType+" Properties");
                        activityDailog.Fields = activityForm.Fields;
                        activityDailog.Show();
                        break;
                }
                //alert("select " + ui.cmd + " on " + $target.text());
            },
            // Implement the beforeOpen callback to dynamically change the entries
            beforeOpen: function (event, ui) {
                var $menu = ui.menu,
                    $target = ui.target;
                $(document)
                    .contextmenu("setEntry", "copy", "Copy '" + $target.text() + "'")
                    .contextmenu("setEntry", "paste", "Paste" + (CLIPBOARD ? " '" + CLIPBOARD + "'" : ""))
                    .contextmenu("enableEntry", "paste", (CLIPBOARD !== ""));
            }
        });

    }
    this.export = function (ServiceUrl) {
        var script = new WorkflowScript();
        if (instance != null) {
            
            $(".statemachine-demo .w,.statemachine-demo .WorkflowStart,.statemachine-demo .Decision").each(
                function (i, activity) {
                    var activity = new WorkflowActivity();
                    activity.Id = this.id;
                    activity.Type = "Task";
                    script.Activites.push(activity);
                });

            var connections = instance.getAllConnections();
            for (var i = 0; i < connections.length; i++) {
                var connection = new WorkflowConnection();
                connection.Source = connections[i].sourceId;
                connection.Target = connections[i].targetId;
                script.Connections.push(connection);
            }
            return script.toXml();
            
        }
    }
}